package auto.app.controller;


import auto.app.aop.RuleValidate;
import auto.app.controller.from.*;
import auto.app.controller.info.CaseDaoDetailInfo;
import auto.app.controller.info.CasePageInfo;
import auto.app.exetion.HibernateExetion;
import auto.app.exetion.MainExection;
import auto.app.service.CaseService;
import auto.app.service.impl.CaseServiceImpl;
import auto.app.util.CommonUtil;
import auto.app.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杨森
 * @since 2022-07-09
 */
@RestController
@RequestMapping("/case")
@Api(value = "测试用例", tags = "测试用例")
public class CaseController {

    @Autowired
    private CaseService caseService;

    //执行用例
    @PostMapping(value = "/callCase", name = "执行用例")
    @ApiOperation(value = "执行用例", notes = "执行用例")
    @RuleValidate
    public Result<String> callCase(@Valid @RequestBody CallCasePO callCasePO) throws MainExection {
        String s = caseService.callCase(callCasePO);
        if (s.equals("执行成功")) {
            return Result.getInstance(String.class).setMsg(s);
        } else {
            return Result.getInstance(String.class).setMsg(s).setCode(500);
        }
    }

    //执行用例
    @PostMapping(value = "/casegeneration/{caseId}", name = "测试用例生成")
    @ApiOperation(value = "测试用例生成", notes = "测试用例生成")
    @RuleValidate
    public Result<Boolean> casegeneration(@PathVariable Integer caseId) throws MainExection {
        Boolean casegeneration = caseService.casegeneration(caseId);
        return Result.getInstance(Boolean.class).setData(casegeneration);
    }

    //获取列表
    @PostMapping(value = "/list", name = "测试用例列表")
    @ApiOperation(value = "测试用例列表", notes = "测试用例列表")
    @RuleValidate
    public Result<CasePageInfo> list(@RequestBody CasePageFrom casePageFrom) {
        CasePageInfo list = caseService.list(casePageFrom);
        return Result.getInstance(CasePageInfo.class).setData(list);
    }

    //新增测试用例
    @PostMapping(value = "/add", name = "新增测试用例")
    @ApiOperation(value = "新增测试用例", notes = "新增测试用例")
    @RuleValidate
    public Result<Boolean> add(@Valid @RequestBody CaseAddFrom caseAddFrom) throws MainExection {
        Boolean aBoolean = caseService.caseAddFrom(caseAddFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    //删除测试用例
    @PostMapping(value = "/delete", name = "删除测试用例")
    @ApiOperation(value = "删除测试用例", notes = "删除测试用例")
    @RuleValidate
    public Result<Boolean> deleteList(@Valid @RequestBody CaseDeleteListFrom caseDeleteListFrom) throws MainExection {
        Boolean aBoolean = caseService.deleteList(caseDeleteListFrom);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

    //查看详情测试用例
    @GetMapping(value = "/detail/{caseId}", name = "查看详情测试用例")
    @ApiOperation(value = "查看详情测试用例", notes = "查看详情测试用例")
    @RuleValidate
    public Result<CaseDaoDetailInfo> detail(@PathVariable Integer caseId) throws MainExection {
        CaseDaoDetailInfo detail = caseService.detail(caseId);
        return Result.getInstance(CaseDaoDetailInfo.class).setData(detail);
    }

    //测试用例复制
    @PostMapping(value = "/copycase", name = "测试用例复制")
    @ApiOperation(value = "测试用例复制", notes = "测试用例复制")
    @RuleValidate
    public Result<Boolean> copyCase(@RequestBody CaseCopySave caseCopySave) throws MainExection {
        Boolean aBoolean = caseService.copyCase(caseCopySave);
        return Result.getInstance(Boolean.class).setData(aBoolean);
    }

}

